import Vue from 'vue'
import Vuex from 'vuex'
import {getHotMovies, getupMovies, setHotMovies, setupMovies} from '../assets/js/cache'
import logger from 'vuex/dist/logger'

import {getMovies, getComingSoon} from '@/api/home'

Vue.use(Vuex)

const debug = process.env.NODE_ENV !== 'production'

export default new Vuex.Store({
  state: {
    hotMovies: getHotMovies(), // 正在热影
    upMovies: getupMovies()   // 正在上映
  },
  getters: {
    hotMovies: state => state.hotMovies,
    upMovies: state => state.upMovies,
  },
  mutations: {
    setHotMovies(state, movies){
      console.log(setHotMovies(movies))
      state.hotMovies = setHotMovies(movies)
    },
    setupMovies(state, movies){

      state.upMovies = setupMovies(movies)
    }
  },
  actions: {
    async  setHotMovies({state, commit}){
      if (state.hotMovies === '') {
        let res = await getMovies()
        if (res) {
          commit('setHotMovies', res.data)
        }
      }
    },
    async setupMovies({state, commit}){
      if (state.upMovies === '') {
        let res = await getComingSoon()
        if (res) {
          commit('setupMovies', res.data)
        }
      }
    }
  },
  strict: debug,
  plugins: debug ? [logger()] : []   // Vuex 自带一个日志插件用于一般的调试
})
